using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using log4net;
using Newtonsoft.Json;
using Triton.Common;
using Triton.Common.LogUtilities;
using Triton.Game;
using Triton.Game.Mapping;

namespace Triton.Bot.Logic.Plugins
{
	public class ExportCollection : IRunnable, IAuthored, IBase, IConfigurable, IEnableable, IPlugin
	{
		private static readonly ILog ilog_0 = Triton.Common.LogUtilities.Logger.GetLoggerInstanceForType();

		private bool bool_0;

		private UserControl userControl_0;

		public string Name => "ExportCollection";

		public string Description => "A simple plugin to export the cards in your collection to a json file when in the Collection Manager.";

		public string Author => "Bossland GmbH";

		public string Version => "0.0.1.1";

		public JsonSettings Settings => ExportCollectionSettings.Instance;

		public UserControl Control
		{
			get
			{
				if (userControl_0 != null)
				{
					return userControl_0;
				}
				using (FileStream stream = new FileStream("Plugins\\ExportCollection\\SettingsGui.xaml", FileMode.Open))
				{
					UserControl root = (UserControl)XamlReader.Load(stream);
					Wpf.FindControlByName<Button>(root, "SaveButton").Click += method_0;
					userControl_0 = root;
				}
				return userControl_0;
			}
		}

		public bool IsEnabled => bool_0;

		public void Initialize()
		{
			ilog_0.DebugFormat("[ExportCollection] Initialize");
		}

		public void Start()
		{
			ilog_0.DebugFormat("[ExportCollection] Start");
		}

		public void Tick()
		{
		}

		public void Stop()
		{
			ilog_0.DebugFormat("[ExportCollection] Stop");
		}

		public void Enable()
		{
			ilog_0.DebugFormat("[ExportCollection] Enable");
			bool_0 = true;
		}

		public void Disable()
		{
			ilog_0.DebugFormat("[ExportCollection] Disable");
			bool_0 = false;
		}

		public void Deinitialize()
		{
		}

		public override string ToString()
		{
			return Name + ": " + Description;
		}

		private void method_0(object sender, RoutedEventArgs e)
		{
			StringBuilder stringBuilder = new StringBuilder();
			using (TritonHs.AcquireFrame())
			{
				CollectionManager collectionManager = CollectionManager.Get();
				if (collectionManager == null)
				{
					return;
				}
				List<CollectibleCard> ownedCards = collectionManager.GetOwnedCards();
				if (ownedCards == null)
				{
					return;
				}
				List<CardWrapper> list = new List<CardWrapper>();
				foreach (CollectibleCard item in ownedCards)
				{
					list.Add(new CardWrapper(item));
				}
				File.WriteAllText("collection.json", JsonConvert.SerializeObject(list, Formatting.Indented));
			}
			ilog_0.Info(stringBuilder.ToString());
		}
	}
}
